package com.springsummer.information.green.beans;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SimpleSingletonBeanRegistryImpl implements SingletonBeanRegistry {

    protected List<String> singletonBeanNames = new ArrayList<>(256);

    protected final Map<String, Object> singletonObjects = new ConcurrentHashMap<>(256);

    private final Map<String, Object> earlySingletonObjects = new ConcurrentHashMap<>(256);


    @Override
    public void registerSingleton(String beanName, Object singletonObject) {
        synchronized (singletonObjects) {
            singletonBeanNames.add(beanName);
            singletonObjects.put(beanName, singletonObject);
        }
    }

    @Override
    public Object getSingleton(String beanName) {
        return singletonObjects.get(beanName);
    }

    @Override
    public boolean containsSingleton(String beanName) {
        return singletonObjects.containsKey(beanName);
    }

    protected void removeSingleton(String beanName) {
        synchronized (this.singletonObjects) {
            this.singletonBeanNames.remove(beanName);
            this.singletonObjects.remove(beanName);
        }
    }

    @Override
    public String[] getSingletonNames() {
        return (String[]) this.singletonBeanNames.toArray();
    }

    @Override
    public int getSingletonCount() {
        return singletonObjects.size();
    }
}
